package com.ly.blog2Admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.ly.blog2Admin.dao.model.SysLog;
import com.ly.blog2Admin.service.SysLogService;
import com.ly.blog2Admin.utils.SecurityUtils;
import com.ly.blog2Common.core.http.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: lyMaster
 * @time: 2020/7/17 7:16
 */

@RestController
@RequestMapping("admin")
public class CommController { //Comm : 通信 - communication

    @Autowired
    private SysLogService   sysLogService;

    @GetMapping("/getLoginUserName")
    public String getLoginUserName(){
        return SecurityUtils.getUsername();
    }

    //采用接口日志记录形式
    @GetMapping("/saveVisitor")
    public JSONObject saveVisitor(@RequestParam(value = "operation",required = false)String operation
                                ,@RequestParam(value = "ip",required = false)String ip
                                ,@RequestParam(value = "method",required = false)String method){
        SysLog sysLog = new SysLog();
        sysLog.setUserName(getLoginUserName());
        sysLog.setIp(ip);
        sysLog.setOperation(operation);
        sysLog.setMethod(method);
        sysLog.setTime((long) 0);
        return ResponseMessage.ok(sysLogService.save(sysLog));
    }


}
